package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.LogBean;
import com.project.bean.UserBean;
import com.project.mapper.IGradeMapper;
import com.project.mapper.ILogMapper;
import com.project.mapper.IRoleMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import com.project.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IRoleMapper roleMapper;
    @Autowired
    private IGradeMapper gradeMapper;
    @Autowired
    private ILogMapper  logMapper;
    @Override
    public UserBean login(String userName, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName",userName);
        qw.eq("u_pwd",pwd);
        UserBean userBean = userMapper.selectOne(qw);
        if (userBean!=null){
            userBean.setRoleBeans( roleMapper.findByUser(userBean.getId()));
            userBean.setGradeSet(gradeMapper.findByUser(userBean.getId()));
            LogBean logBean = new LogBean();
            LocalTime now = LocalTime.now();
            String date = DateUtil.getDate(now);
            logBean.setInfo("用户"+userBean.getRealName()+"在"+date+"登录系统");
            logMapper.add(logBean);
            return userBean;
        }
        return null;
    }

    @Override
    public boolean add(UserBean user, Integer[] roleIdArray) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_userName",user.getUserName());
        Integer integer = userMapper.selectCount(qw);
        if (integer==0){
            userMapper.insert(user);
            userMapper.addCenter(user.getId(), roleIdArray);
            return true;
        }
        return false;
    }

    @Override
    public IPage<UserBean> findByItem(Integer pageNO, String userName) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if (userName!=null&&userName.length()!=0){
            qw.like("u_userName",userName);
        }
        return userMapper.selectPage(new Page(pageNO,3),qw);
    }

    @Override
    public void updatePwd(Integer id, String pwd) {
        UserBean userBean = userMapper.selectById(id);
        userBean.setPwd(pwd);
        userMapper.updateById(userBean);
    }

    @Override
    public boolean del(Integer id, UserBean loginUser) {
        QueryWrapper<UserBean > qw = new QueryWrapper<>();
        qw.eq("pk_userId",loginUser.getId());
        UserBean userBean = userMapper.selectOne(qw);
        if (userBean!=null){
            userMapper.del(id);
            userMapper.deleteById(id);
        }
        return false;
    }

    @Override
    public UserBean findById(Integer id) {
        UserBean userBean = userMapper.selectById(id);
        return userBean;
    }

    @Override
    public void updateRoles(Integer id, Integer[] rolesId) {
        userMapper.updateRole(id,rolesId);
    }
}
